package com.generate.table;


import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Tr {
    private List<Td> tdList = new LinkedList<>();

    public List<Td> getTdList() {
        return tdList;
    }

    public void setTdList(List<Td> tdList) {
        this.tdList = tdList;
    }

    public void addTd(Td td) {
        this.tdList.add(td);
    }

    public List<Td> addTdByObj(Object obj) {
        List<Td> tds = new LinkedList<>();
        Class<?> aClass = obj.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            try {
                if (!declaredField.getName().contains("$")) {
                    Object o = declaredField.get(obj);
                    List<Td> tds1 = null;
                    if (o == null) {
                        tds1 = addTd("");
                    } else {
                        tds1 = addTd(o);
                    }
                    tds.addAll(tds1);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            declaredField.setAccessible(false);
        }
        return tds;
    }


    public void addTd(Td... tds) {
        if (tds != null && tds.length > 0) {
            Arrays.stream(tds).forEach(this::addTd);
        }
    }

    public void addTd(String[] titles, Integer[][] spans) {
        if (titles != null && titles.length > 0) {
            for (int i = 0; i < titles.length; i++) {
                Td td = Td.create(titles[i]).setRowSpan(spans[i][0]).setColsSpan(spans[i][1]);
                this.addTd(td);
            }
        }
    }

    public void addTd(BigDecimal[] titles, Integer[][] spans) {
        if (titles != null && titles.length > 0) {
            for (int i = 0; i < titles.length; i++) {
                Td td = Td.create(BigDecimalUtil.formatToNumber(titles[i])).setRowSpan(spans[i][0]).setColsSpan(spans[i][1]);
                this.addTd(td);
            }
        }
    }

    public void addTd(String[] titles) {
        if (titles != null && titles.length > 0) {
            for (int i = 0; i < titles.length; i++) {
                Td td = Td.create(titles[i]).setRowSpan(1).setColsSpan(1);
                this.addTd(td);
            }
        }
    }

    public void addTd(String[] titles, int rowSpan, int colsSpan) {
        if (titles != null && titles.length > 0) {
            for (int i = 0; i < titles.length; i++) {
                Td td = Td.create(titles[i]).setRowSpan(rowSpan).setColsSpan(colsSpan);
                this.addTd(td);
            }
        }
    }

    public void addTd(BigDecimal... titles) {
        if (titles != null && titles.length > 0) {
            for (int i = 0; i < titles.length; i++) {
                Td td = Td.create(BigDecimalUtil.formatToNumber(titles[i])).setRowSpan(1).setColsSpan(1);
                this.addTd(td);
            }
        }
    }

    public List<Td> addTd(Object... titles) {
        List<Td> tds = new LinkedList<>();
        if (titles != null && titles.length > 0) {
            for (int i = 0; i < titles.length; i++) {
                String value = "";
                Object obj = titles[i];
                if (obj instanceof String || obj instanceof Integer) {
                    value = String.valueOf(obj);
                    Td td = Td.create(value).setRowSpan(1).setColsSpan(1);
                    tds.add(td);
                    this.addTd(td);
                }
                if (obj instanceof BigDecimal) {
                    value = BigDecimalUtil.formatToNumber((BigDecimal) obj);
                    Td td = Td.create(value).setRowSpan(1).setColsSpan(1);
                    tds.add(td);
                    this.addTd(td);
                }
                if (obj instanceof Td) {
                    Td td = ((Td) obj);
                    tds.add(td);
                    this.addTd(td);
                }
            }
        }
        return tds;
    }
}
